package com.file;

import java.io.File;
import java.text.BreakIterator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weijingchen
 * @DateTime 2025-06-16
 */
public class Test01 {
    public static void main(String[] args) {
        File file = new File("D:\\home\\nomp\\services\\log");
        HashMap<String, Integer> map = new HashMap<>();
        test01(file, map);
        System.out.println(map);
    }

    public static void delete(File file) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                if (file1.isFile()) {
                    file1.delete();
                } else {
                    delete(file1);
                }
            }
            file.delete();
        }
    }

    /**
     * 统计文件
     * @param file 文件路径
     * @param map 存储文件类型和文件数量
     */
    public static void test01(File file, Map<String, Integer> map) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                if (file1.isFile()) {
                    String name = file1.getName();
                    int i = name.lastIndexOf(".");
                    String suffix = name.substring(i);
                    if (map.containsKey(suffix)) {
                        int count = map.get(suffix) + 1;
                        map.put(suffix, count);
                    } else {
                        map.put(suffix, 1);
                    }
                } else {
                    test01(file1, map);
                }
            }
        }
    }
}
